package model;

import java.util.ArrayList;
import java.io.*;
import java.util.Observable;
import java.util.Observer;

public class Maid extends Observable{
    private ArrayList<String> dustBunnies = new ArrayList();
    private File startingPath;
    private ArrayList<File> dirList = new ArrayList();
    private boolean traverse = true;
    private boolean moveToFolder = true;
    private String folderName = "DELETE ME";
    private File deletedFolder;

    public Maid(){}

    public Maid(String path){
        startingPath = new File(path);
    }

    public void setTraversal(boolean val){
        traverse = val;
    }

    public void setMoveToFolder(boolean val){
        moveToFolder = val;
    }

    public void addDustBunny(String fileExt){
        char firstChar = fileExt.charAt(0);
        if(firstChar == '.')
            dustBunnies.add(fileExt.substring(1));
        else
            dustBunnies.add(fileExt);
    }

    public void addDustBunnies(String[] fileExts){
            for(int i=0; i<fileExts.length; i++){
                if(!fileExts[i].isEmpty()){
                    char firstChar = fileExts[i].charAt(0);
                    if(firstChar == '.')
                        dustBunnies.add(fileExts[i].substring(1));
                    else
                        dustBunnies.add(fileExts[i]);
                }
            }
    }

    public void removeDustBunny(String fileExt){
        dustBunnies.remove(fileExt);
    }

    public void clearDustBunnies(){
        dustBunnies.clear();
    }

    public void setStartingPath(String path){
        startingPath = new File(path);
    }

    public void setStartingPath(File path){
        startingPath = path;
    }

    public void changeFolderName(String name){
        this.folderName = name;
    }

    /*
     * Main Logic
     */
    public void clean(){
        if(dustBunnies.isEmpty()){
            createLogEvent("You need to add a file extention");
        }else if(startingPath == null){
            createLogEvent("You need to specify a starting path");
        } else {
            if(moveToFolder && !folderName.isEmpty()){
                if(new File(startingPath.getAbsolutePath() + "/" + folderName).exists()){
                    deletedFolder = new File(startingPath.getAbsolutePath() + "/" + folderName);
                } else {
                    boolean success = new File(startingPath.getAbsolutePath() + "/" + folderName).mkdir();
                    if(success)
                        deletedFolder = new File(startingPath.getAbsolutePath() + "/" + folderName);
                }
            }

            createLogEvent("Starting cleaning...");
            cleanDirectory(startingPath);
            createLogEvent("Done cleaning...");
        }
    }

    private void cleanDirectory(File path){
        File[] files = path.listFiles();

        for(File elem : files){
            if(elem.isDirectory() && traverse)
                cleanDirectory(elem);

            if(dustBunnies.contains(this.getFileExtention(elem))){
                this.createLogEvent("Cleaning " + elem.getName());
                if(moveToFolder && deletedFolder != null)
                    elem.renameTo(new File(deletedFolder,elem.getName()));
                else if(moveToFolder && deletedFolder == null)
                    System.out.println("The delete folder was not set correctly");
                else
                    elem.delete();
            }

        }
    }

    private String getFileExtention(File file){
        String filename = file.getName();
        int dotPosition = filename.lastIndexOf(".");

        if(dotPosition<0)
            return "";
        else
            return filename.substring(dotPosition+1);
    }

    private void createLogEvent(String message){
        this.setChanged();
        this.notifyObservers(message);
    }
}
